package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.LoansDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ListenerWithPosition;
import com.cardbaobao.cardbabyclient.model.SpeedOrProduct;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Created by FHW
 * on 2017/6/13
 */

public class SpeedLoanProductRcAdapter extends CommonRecyclerAdapter<SpeedOrProduct.LoanListBean> implements ListenerWithPosition.OnClickWithPositionListener<CommonRecyclerHolder> {

    private long enterTime;
    private String currentPageId;
    private String prePageId;

    public SpeedLoanProductRcAdapter(Context context) {
        super(context, null, R.layout.item_speed_loan_rv);
    }

    public void setBuriedInfo(long enterTime, String currentPageId, String prePageId){
        this.enterTime=enterTime;
        this.currentPageId=currentPageId;
        this.prePageId=prePageId;
    }

    @Override
    public void onClick(View v, int position, CommonRecyclerHolder holder) {
        ToastUtil.showToast(position + "");
    }

    @Override
    public void convert(CommonRecyclerHolder holder, final SpeedOrProduct.LoanListBean homeTabBean) {
        if(holder.getAdapterPosition()==0){
            ((ViewGroup.MarginLayoutParams)holder.itemView.getLayoutParams()).topMargin=
                    DensityUtil.dp2px(5);
        }
        ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL + homeTabBean.getImage(),
                (ImageView) holder.getView(R.id.iv_pic),
                R.mipmap.img_default_square);
        holder.getView(R.id.iv_recommend).setVisibility(View.GONE);
        holder.getView(R.id.iv_hot).setVisibility(View.GONE);
        holder.getView(R.id.iv_new).setVisibility(View.GONE);
        if (homeTabBean.getTopTag() != null) {
            for (String s : homeTabBean.getTopTag()) {
                switch (s) {
//                    case Constant.JBQ0103020101://新
                    case "新"://新
                        holder.getView(R.id.iv_new).setVisibility(View.VISIBLE);
                        break;
//                    case Constant.JBQ0103020102://热
                    case "热"://热
                        holder.getView(R.id.iv_hot).setVisibility(View.VISIBLE);
                        break;
//                    case Constant.JBQ0103020103://荐
                    case "荐"://荐
                        holder.getView(R.id.iv_recommend).setVisibility(View.VISIBLE);
                        break;
//                    default:
//                        holder.getView(R.id.iv_recommend).setVisibility(View.GONE);
//                        holder.getView(R.id.iv_hot).setVisibility(View.GONE);
//                        holder.getView(R.id.iv_new).setVisibility(View.GONE);
//                        break;
                }
            }
        }

        holder.setTextViewText(R.id.tv_title, homeTabBean.getName());
//        holder.setTextViewText(R.id.tv_marketing_one, homeTabBean.getRateDescription());
        holder.setTextViewText(R.id.tv_marketing_one, homeTabBean.getFeature());

        FlowLayout fl_label = holder.getView(R.id.fl_label);
//        String[] str = {"红包", "利率低", "高额度"};
        if (homeTabBean.getBottomTag() != null) {
            //去空
            List<String> source=homeTabBean.getBottomTag();
            List<String> temp=new ArrayList<>();
            for(String s:source){
                if(!TextUtils.isEmpty(s)){
                    temp.add(s);
                }
            }
            //最多取3个
            List<String> finalStrList;
            if(temp.size()>3){
                finalStrList=temp.subList(0,3);
            }else{
                finalStrList=temp;
            }
            fl_label.setAdapter(new FlowCommonAdapter(mContext, finalStrList, R.layout.item_speed_loan_label_gv));
        }

        holder.setTextViewText(R.id.tv_number, homeTabBean.getTotalApply() + "");

        holder.getView(R.id.rl_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelfUtils.buriedClickData(Self.Action.app_click_speed_loan_item,
                        Self.Action.app_click_speed_loan_item_des,
                        enterTime,
                        currentPageId,
                        prePageId);
                Intent mIntent = new Intent();
                mIntent.setClass(mContext, LoansDetailActivity.class);
                mIntent.putExtra(LoansDetailActivity.LOANS_ID_S,
                        homeTabBean.getID() + "");
                mIntent.putExtra(LoansDetailActivity.LOANS_NAME_S,
                        homeTabBean.getName());
                mIntent.putExtra(LoansDetailActivity.FROM_PAGE_S,
                        mContext.getString(R.string.text_speed_loan));
                mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                        Self.Page.app_fast_loan_page);
                mContext.startActivity(mIntent);
            }
        });

    }

}
